/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;

using Gtk;
using Mono.Unix;

namespace DCSharp.Gui
{
	public abstract class ExtendedTreeView : MenuTreeView
	{
		private bool activateMultipleRows;

		protected ExtendedTreeView() : this(null)
		{
		}

		protected ExtendedTreeView(TreeModel model) : base(model)
		{
#if GTK_2_10
			RubberBanding = true;
#endif
			RulesHint = true;
			Selection.Mode = SelectionMode.Multiple;
		}

		#region Properties

		public bool CanActivateMultipleRows
		{
			get { return activateMultipleRows; }
			set { activateMultipleRows = value; }
		}

		#endregion

		#region Methods

		public TreeViewColumn GetColumn(string title)
		{
			title = Catalog.GetString(title);

			return Array.Find(Columns, delegate(TreeViewColumn column)
			{
				return column.Title == title;
			});
		}

		public object[] GetSelectedObjects(int column)
		{
			TreeIter[] selectedIters = GetSelectedIters();
			object[] objects = new object[selectedIters.Length];

			for (int i = 0; i < selectedIters.Length; i++)
			{
				objects[i] = Model.GetValue(selectedIters[i], column);
			}
			return objects;
		}

		public T[] GetSelectedObjects<T>(int column, bool recurse) where T : class
		{
			TreeIter[] selectedIters = GetSelectedIters();
			List<T> objects = new List<T>();

			foreach (TreeIter iter in selectedIters)
			{
				AddObjectsToList(objects, column, iter, recurse);
			}
			return objects.ToArray();
		}

		private void AddObjectsToList<T>(List<T> objects, int column,
			TreeIter iter, bool recurse) where T : class
		{
			TreeIter childIter;

			T obj = Model.GetValue(iter, column) as T;
			if (obj != null)
			{
				if (!objects.Contains(obj))
				{
					objects.Add(obj);
				}
			}
			else if (recurse && Model.IterChildren(out childIter, iter))
			{
				do
				{
					AddObjectsToList(objects, column, childIter, true);
				}
				while (Model.IterNext(ref childIter));
			}
		}

		public TreeIter[] GetSelectedIters()
		{
			TreePath[] paths = Selection.GetSelectedRows();
			List<TreeIter> iters = new List<TreeIter>();

			foreach (TreePath path in paths)
			{
				TreeIter iter;
				if (Model.GetIter(out iter, path))
				{
					iters.Add(iter);
				}
			}
			return iters.ToArray();
		}

		// By default, only one row is activated when pressing Enter. Overriding.
		protected override bool OnKeyPressEvent(Gdk.EventKey evnt)
		{
			if (activateMultipleRows && evnt.Key == Gdk.Key.Return)
			{
				TreePath[] paths = Selection.GetSelectedRows();
				foreach (TreePath path in paths)
				{
					ActivateRow(path, Columns[0]);
				}
				return true;
			}
			return base.OnKeyPressEvent(evnt);
		}

		#endregion
	}
}
